<?php
namespace app\web\controller;

use app\web\library\WebController;

class Index extends WebController{

    public function onIndex(){
        
        $this->pageCache('index');
        
        $templateConfig = $this->app->config('template',true);
        
        if (!$templateConfig->exists('index_template') || $templateConfig->index_template==''){
            $this->page404();
        }

        return $this->display($templateConfig->index_template);
    }

    public function onPart(){
        $part_id = $this->get('id',$this->check()->type('int')->required());
        $page  = $this->get('page',$this->check()->type('int')->default(1));
        $search = $this->get('search',$this->check()->type('string')->default(''));

        $md5 = md5($part_id.'_'.$page.'_'.$search);

        $this->pageCache('part.'.$md5);

        $part = $this->model('Part');

        $partData  = $part->frontData($part_id);
        
        if (!$partData){
            $this->page404();
        }

        $this->set('part',$partData);
        
        $template  = $partData['row']['part_template'];

        if (!$template){
            $config = $this->app->config('template',true);
            $template = $config->part_template;
            if (!$template) throw new \Exception('未设置栏目模板');
        }

        //装载合法的GET参数
        $this->set('GET',[
            'part_id' => $part_id,
            'page' => $page,
            'search'=> $search
        ]);

        return $this->display($template);
    }

    public function onPage(){
        $page_id = $this->get('id',$this->check()->type('int')->required());

        $this->pageCache('page.'.$page_id);
        $page = $this->model('Page');

        $pageData  = $page->getId($page_id);
        
        if (!$pageData){
            $this->page404();
        }

        $this->set('page',$page->frontData($pageData));

        //装在合法的参数
        
        return $this->display($pageData['template']);
    }
}